package com.suke.czx.modules.tb.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.suke.czx.modules.sys.entity.SysUser;
import com.suke.czx.modules.sys.service.SysUserService;
import com.suke.czx.modules.tb.vo.CommentVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.suke.czx.modules.tb.entity.TbComment;
import com.suke.czx.modules.tb.service.TbCommentService;
import com.suke.czx.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.suke.czx.common.annotation.SysLog;
import com.suke.czx.common.base.AbstractController;

import javax.annotation.Resource;


/**
 * 评论表
 *
 * @author developer
 * @email developer@163.com
 * @date 2025-05-17 21:50:15
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tb/comments")
@Api(value = "TbCommentController", tags = "评论表")
public class TbCommentController extends AbstractController {
    private final TbCommentService tbCommentService;
    @Resource
    private SysUserService sysUserService;
    /**
     * 列表
     */
    @ApiOperation(value = "评论表列表")
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        QueryWrapper<TbComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbComment::getProductId, params.get("productId"));
        final String keyword = mpPageConvert.getKeyword(params);
        if (StrUtil.isNotEmpty(keyword)) {

        }
        IPage<CommentVO> listPage = tbCommentService.commentPage(params);
        return R.ok().setData(listPage);
    }

    /**
     * 新增评论表
     */
    @ApiOperation(value = "新增评论表数据")
    @SysLog("新增评论表数据")
    @PostMapping("/add")
    public R add(@RequestBody TbComment tbComment) {
        if(StringUtils.isBlank(tbComment.getContent())){
            return R.error("请输入内容");
        }
        if(null == tbComment.getProductId() || 0L == tbComment.getProductId()){
            return R.error("参数错误");
        }
        tbComment.setUserId(getUserId());
        tbComment.setCreateDate(new Date());
        tbCommentService.save(tbComment);
        return R.ok();
    }


    /**
     * 新增评论表
     */
    @ApiOperation(value = "新增评论表数据")
    @SysLog("新增评论表数据")
        @PostMapping()
        public R save(@RequestBody TbComment tbComment) {
        tbCommentService.save(tbComment);
        return R.ok();
    }


    /**
     * 修改
     */
    @ApiOperation(value = "修改评论表数据")
    @SysLog("修改评论表数据")
        @PutMapping()
        public R update(@RequestBody TbComment tbComment) {
        tbCommentService.updateById(tbComment);
        return R.ok();
    }


    /**
     * 删除
     */
    @ApiOperation(value = "删除评论表数据")
    @SysLog("删除评论表数据")
        @DeleteMapping()
        public R delete(@RequestBody TbComment tbComment) {
        tbCommentService.removeById(tbComment.getCommentsId());
        return R.ok();
    }

}
